package me.winds.wrapper.base.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.util.ArrayMap;

import java.util.List;


public abstract class CommonFragmentPagerAdapter<T> extends FragmentPagerAdapter {

    protected List<T> list;
    protected ArrayMap<Integer, Fragment> mFragmentMaps = new ArrayMap<>();

    public CommonFragmentPagerAdapter(FragmentManager fm, List<T> list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        T data = list.get(position);
        Fragment fragment = mFragmentMaps.get(position);

        if (fragment == null) {
            fragment = initFragment(data);
            mFragmentMaps.put(position, fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    protected abstract Fragment initFragment(T data);

    public List<T> getAdapterData() {
        return list;
    }

    public void setAdapterData(List<T> list) {
        this.list = list;
    }


}
